<%@ page pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" %>
<%@ include file="/common/common.jsp" %>
<%
    String prjid=request.getParameter("prjid");
%>
<head>
<title></title>
<%@ include file="/common/head.jsp" %>
<link rel="stylesheet" type="text/css" href="${ctx}/js/ext/resources/css/ext-all.css"/>
<link href="${ctx}/ligerskins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css"/>
<link href="${ctx}/ligerskins/ligerui-icons.css" rel="stylesheet" type="text/css"/>
<script src="${ctx}/js/liger/core/base.js" type="text/javascript"></script>
<script src="${ctx}/js/liger/plugins/ligerGrid.js" type="text/javascript"></script>
<script src="${ctx}/js/liger/plugins/ligerTextBox.js" type="text/javascript"></script>
<script src="${ctx}/js/liger/plugins/ligerDialog.js" type="text/javascript"></script>
<script src="${ctx}/js/liger/plugins/ligerToolBar.js" type="text/javascript"></script>
<script src="${ctx}/js/jquery.mask.js" type="text/javascript"></script>
<style type="text/css">
    .l-grid-row-cell-inner {overflow:visible;}
</style>
<style type="text/css">

/* barbox */
    .barbox{width:100px;background:#E3E3E3;height:12px;overflow:hidden;margin:3px 10px 0 10px;display:inline;border-bottom:solid 1px #F4F4F4;border-right:solid 1px #F4F4F4;}

    .barbox dd.barline div.charts{height:10px;overflow:hidden;background:url(/images/barbg.gif) repeat-x;}
    .barbox dd.barline div.charts.barred{background-position:0 0;border:1px solid #C2142C;}
    .barbox dd.barline div.charts.baryellow{background-position:0 -10px;border:1px solid #EFA804;}
    .barbox dd.barline div.charts.barblue{background-position:0 -20px;border:1px solid #0A83E6;}
</style>
<script type="text/javascript">
delete Array.prototype.indexOf;
delete Array.prototype.remove;
var testdata = {"rows":[
    {"children":[
        {"children":[], "processid":"40895a003656e99f013657fbdf9200d4", "taskname":"售前", "id":"2"},
        {"children":[
            {"children":[], "processid":"40895a003656e99f01365806d36d00ea", "taskname":"开启动会", "uptask":"40895a003656e99f013657fc1c5200df", "id":"1.1"}
        ], "processid":"40895a003656e99f013657fc1c5200df", "taskname":"启动", "id":"1"}
    ], "processid":"", "taskname":"任务分解"}
]};


function onSave(item) {
    data = manager.getData();
    $(document).mask('正在保存...');
    $.ajax({
        data:{tasks:JSON2.stringify(data)},
        async:true,
        type:'POST',
        url:'${ctx}/ext/com.cinsea.project.ext.ProjectAction?action=savetasks',
        success:function (data) {
            manager.changedCells = [];
            manager.loadData(true);
            $(document).unmask();
        },
        error:function (data) {

        }
    });

}

var manager;
$(function () {
    //menu
    $("#toptoolbar").ligerToolBar({ items:[
        { text:'保存', click:onSave, icon:'save', disable:true},
        { text:'导入MPP', click:showWindowupload,  icon:'communication', disable:true}
    ]
    });
    //grid
    manager = $("#maingrid").ligerGrid({
                columns:[

                    { display:'wbs', name:'id', width:50, type:'int', align:'left', editor:{ type:'text'} },
                    { display:'任务名称', name:'taskname', width:250, align:'left' },
                    { display:'操作', name:'op', width:120, align:'left', render:function (rowdata, rowindex, value) {
                        var h = "";

                        if (!rowdata._editing) {
                            if (rowindex == 0) {
                                h += "<img style='cursor: hand' height='18' src='/images/base/add.gif' onclick='beginAdd(" + rowindex + ")'> ";
                                return h;
                            }
                            h += "<img style='cursor: hand' height='18' src='/images/base/add.gif' onclick='beginAdd(" + rowindex + ")'> ";
                            h += "<img style='cursor: hand' height='18' src='/images/base/edit.gif' onclick='beginEdit(" + rowindex + ")'>";
                            h += "<img style='cursor: hand' height='18' src='/images/base/delete.gif' onclick='deleteRow(" + rowindex + ")'>";
                            var parentrow = this.getParent(rowindex);
                            if (parentrow != null && (rowindex - parentrow.__index) > 1)
                                h += "<img style='cursor: hand' height='18' src='/images/base/moveup.gif' onclick='javascript:up(" + rowindex + ")'>";
                            else
                                h += "<img  height='18' src='/images/base/moveup1.gif'>";

                            var thisrow = this.getRow(rowindex);
                            var listdata = this._getParentChildren(thisrow);
                            var index = $.inArray(thisrow, listdata);
                            if (index == -1 || index == listdata.length - 1)
                                h += "<img  height='18' src='/images/base/movedown1.gif'>";
                            else
                                h += "<img style='cursor: hand' height='18' src='/images/base/movedown.gif' onclick='javascript:down(" + rowindex + ")'>";
                        }
                        else {
                            h += "<a href='javascript:endEdit(" + rowindex + ")'>提交</a> ";
                            h += "<a href='javascript:cancelEdit(" + rowindex + ")'>取消</a> ";
                        }
                        return h;
                    }
                    },
                    { display:'责任人', name:'master', width:100, align:'center' },
                    { display:'完成进度', name:'wcjd', width:160},
                    { display:'输出文档', name:'enterdocs', width:150,align:'left'},
                    { display:'开始日期', name:'planstartdate', width:100 },
                    { display:'结束日期', name:'planenddate', width:100 }
                ], width:'100%', usePager:false, height:'97%',
                onSelectRow:function (rowdata, rowindex) {
                    $("#txtrowindex").val(rowindex);
                },
                url:'${ctx}/ext/com.cinsea.project.ext.ProjectAction?action=gettasklist&prjid=<%=prjid%>', alternatingRow:false, tree:{ columnName:'taskname' }, checkbox:false, rownumbers:false,
                enabledEdit:true, clickToEdit:true, isScroll:false,
                autoCheckChildren:false
            }
    );

    var uploadform =new  Ext.FormPanel({
        region:'center',
        id:'uploadform',
        fileUpload:true,
        frame:true,
        labelWidth :100,
        autoScroll:true,
        items:[{
            fieldLabel:"上传文件",
            xtype:'textfield',
            allowBlank:false,
            inputType:'file'
        }]
    });

    var uploadwindow =new Ext.Window({
        title:'上传文件',
        layout:'border',
        closeAction:'hide',
        frame:true,
        plain: true,
        modal :true,
        width:400,
        height:300,
        buttons:[{
            text:'提交',
            handler:function(){
                $(document).mask('正在导入...');
                uploadform.form.submit({
                    url:'<%=request.getContextPath()%>/ext/com.cinsea.project.ext.ProjectAction?action=upload_mpp&prjid=<%=prjid%>',
                    success:function(f,a){
                        $(document).unmask();
                        uploadwindow.hide();
                        pop("文件上传成功！");
                    },
                    failure:function(f, a) {
                        switch (a.failureType) {
                            case Ext.form.Action.CLIENT_INVALID :
                                pop('<%=I18n.getText("validatefail")%>!', '<%=I18n.getText("error")%>', 5, 'cross');
                                break;
                            case Ext.form.Action.CONNECT_FAILURE :
                                pop('<%=I18n.getText("connectfail")%>!', '<%=I18n.getText("error")%>', 5, 'cross');
                                break;
                            case Ext.form.Action.SERVER_INVALID :
                                pop(a.result.errors, '<%=I18n.getText("error")%>', 5, 'cross');
                        }
                    }
                })
            }
        },
        {
            text  : '关闭',
            handler : function() {
                uploadwindow.hide();
            }
        }],
        items:[uploadform]
    });
    uploadwindow.render(Ext.getBody());

    function showWindowupload(){
        uploadwindow.show();
    }
});

function getFilepath(path){
    if(path=='success'){
        $.ajax({
            type:'POST',
            sync:true,
            url:'${ctx}/ext/com.cinsea.project.ext.ProjectAction?action=gettasklist&prjid=<%=prjid%>',
            success:function(){
                manager.changedCells = [];
                manager.loadData(true);
            }
        });
    }
}

function beginAdd(rowid) {
    data = manager.getData();
    var row = manager.getRow(rowid);
    var taskid = row.processid;
    //先保存变更
    $.ajax({
        data:{tasks:JSON2.stringify(data)},
        async:true,
        type:'POST',
        url:'${ctx}/ext/com.cinsea.project.ext.ProjectAction?action=savetasks',
        success:function (data) {
            manager.changedCells = [];
            id = showDialog('${ctx}/form/formprocess.jsp?directoryid=40895a003652f4f9013653964e6c010c&prjid=<%=prjid%>&parentid='+taskid, null, null, {onCancel:function () {
                manager.loadData(true);
            },onClose:function(){
                dg.cancel()
                manager.loadData(true);
            }});
        },
        error:function (data) {

        }
    });


}
function beginEdit(rowid) {
    //manager.beginEdit(rowid);
    data = manager.getData();
    //var row = manager.getSelectedRow();
    var row=manager.getRow(rowid);
    var taskid = row.processid;
    //先保存变更
    $.ajax({
        data:{tasks:JSON2.stringify(data)},
        async:true,
        type:'POST',
        url:'${ctx}/ext/com.cinsea.project.ext.ProjectAction?action=savetasks',
        success:function (data) {
            manager.changedCells = [];

            id = showDialog('${ctx}/form/formprocess.jsp?processid=' + taskid, null, null, function () {
                manager.loadData(true);
            });

        },
        error:function (data) {

        }
    });


}
function open(taskid) {
    data = manager.getData();
    $.ajax({
        data:{tasks:JSON2.stringify(data)},
        async:true,
        type:'POST',
        url:'${ctx}/ext/com.cinsea.project.ext.ProjectAction?action=savetasks',
        success:function (data) {
            manager.changedCells = [];

            id = showDialog('${ctx}/form/formprocess.jsp?processid=' + taskid, null, null, function () {
                manager.loadData(true);
            });

        },
        error:function (data) {

        }
    });


}
function cancelEdit(rowid) {
    manager.cancelEdit(rowid);
}
function endEdit(rowid) {
    manager.endEdit(rowid);
}


function deleteRow(rowid) {
    //var row = manager.getSelectedRow();
    var row=manager.getRow(rowid);
    if (confirm('确定删除?')) {
        $(document).mask('正在删除...');
        data = manager.getData();
        $.ajax({
            data:{taskid:row.processid, tasks:JSON2.stringify(data)},
            async:true,
            type:'POST',
            url:'${ctx}/ext/com.cinsea.project.ext.ProjectAction?action=delete',
            success:function (data) {
                manager.changedCells = [];
                manager.loadData(true);
                $(document).unmask();
            },
            error:function (data) {

            }
        });
    }
    //manager.deleteRow(row);
}
function toggle() {
    var row = manager.getSelected();
    manager.toggle(row);
}
function expand() {
    var row = manager.getSelected();
    manager.expand(row);
}
function up(rowid) {
    //var row = manager.getSelected();
    var row=manager.getRow(rowid);
    var prevrow = manager.getRow(row.__index - 1);
    var parentrow = manager.getParent(row.__index)
    var prevparentrow = manager.getRow(prevrow.__index - 1);
    if (parentrow.__index != prevparentrow.__index) {//相邻节点非兄弟节点
        var listdata = manager._getParentChildren(row);
        var index = $.inArray(row, listdata);
        prevrow = listdata[index - 1];
    }
    var thisid = row.id;
    manager.up(row);
    manager.updateCell('id', prevrow.id, row);
    manager.updateCell('id', thisid, prevrow);
    column = manager.columns[0];
    cellObj = manager.getCellObj(row, column);
    $(cellObj).addClass("l-grid-row-cell-edited");
    prevcellObj = manager.getCellObj(prevrow, column);
    $(prevcellObj).addClass("l-grid-row-cell-edited");
}
function down(rowid) {
    //var row = manager.getSelected();
    var row=manager.getRow(rowid);
    var nextrow = manager.getRow(row.__index + 1);
    var parentrow = manager.getParent(row.__index)
    var nextparentrow = manager.getRow(nextrow.__index - 1);
    if (parentrow.__index != nextparentrow.__index) {//相邻节点非兄弟节点
        var listdata = manager._getParentChildren(row);
        var index = $.inArray(row, listdata);
        nextrow = listdata[index + 1];
    }
    var thisid = row.id;
    //alert(JSON2.stringify(row))
    manager.down(row);
    manager.updateCell('id', nextrow.id, row);
    manager.updateCell('id', thisid, nextrow);
    column = manager.columns[0];
    cellObj = manager.getCellObj(row, column);
    $(cellObj).addClass("l-grid-row-cell-edited");
    nextcellObj = manager.getCellObj(nextrow, column);
    $(nextcellObj).addClass("l-grid-row-cell-edited");
}

function animate(){
	var max="barred";
	var middle="baryellow";
	var min="barblue";
	$(".charts").each(function(i,item){
		var addStyle="";
        var a=parseInt($(item).attr("w"));
		if(a>80){
			addStyle=max;
		}else if(a<50){
			addStyle=min;
		}else{
			addStyle=middle;
		}

		$(item).addClass(addStyle);
		var a=$(item).attr("w");
		$(item).animate({
			width: a+"%"
		},1000);
	});
}
</script>
</head>
<body style="padding:4px; padding-top: 0px;">
<div id="toptoolbar"></div>
<div id="maingrid">
</div>
<div>
</div>
</body>
</html>
